<?php
namespace App\Http\Controllers;

use App\Http\Logic\TradeLogic;
use App\Http\Requests\Trade\HandleTransferRequest;
use App\Http\Requests\Trade\QueryBalanceRequest;
use App\Http\Requests\Trade\QueryCoinBalanceRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TradeController extends BaseController
{

    /**
     * @notes 获取账户资产
     * @param \App\Http\Requests\Trade\QueryBalanceRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/11 16:07
     */
    public function queryBalance(QueryBalanceRequest $request) : JsonResponse
    {
        $quote = $request->input('quote_ccy');

        $user_id = auth() -> user() -> id;

//        $total_0 = TradeLogic::queryBalance($user_id, 0, $quote);
//        if(false === $total_0) {
//            return $this->error(TradeLogic::getErrorMessage());
//        }

        $total_1 = TradeLogic::queryBalance($user_id, 1, $quote);
        if(false === $total_1) {
            return $this->error(TradeLogic::getErrorMessage());
        }

//        $data = [
//            'total' => $total_1,
//            'total_0' => $total_0,
//            'total_1' => $total_1
//        ];

        return $this->success($total_1);
    }

    /**
     * @notes 获得资产列表
     * @param QueryBalanceRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/21 13:22
     */
    public function queryAddressBalance(QueryBalanceRequest $request) : JsonResponse
    {
        $quote = $request->input('quote_ccy');

        $account_type = $request->input('account_type') ?? '';

        $user_id = auth() -> user() -> id;

        $list = TradeLogic::queryAddressBalance($user_id, $quote, $account_type);

        if(false === $list) {
            return $this->error(TradeLogic::getErrorMessage());
        }

        return $this->success($list);
    }

    public function queryCoinBalance(QueryCoinBalanceRequest $request) : JsonResponse
    {
        $coin_id = $request->input('coin_id');

        $account_type = $request->input('account_type');

        $user_id = auth() -> user() -> id;

        $list = TradeLogic::queryCoinBalance($user_id, $coin_id, $account_type);

        if(false === $list) {
            return $this->error(TradeLogic::getErrorMessage());
        }

        return $this->success($list);
    }

    /**
     * @notes 获取可交易的资产余额
     * @param QueryBalanceRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/22 14:08
     */
    public function queryBuyBalance(QueryBalanceRequest $request) : JsonResponse
    {
        $quote = $request->input('quote_ccy');

        $base = $request->input('base_ccy');

        $user_id = auth() -> user() -> id;

        $list = TradeLogic::queryBuyBalance($user_id, $quote, $base);

        if(false === $list) {
            return $this->error(TradeLogic::getErrorMessage());
        }

        return $this->success($list);
    }

    /**
     * @notes 获取交易记录
     * @param Request $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/21 14:18
     */
    public function queryLogs(Request $request) : JsonResponse
    {
        $user_id = auth() -> user() -> id;

        $pageNo = $request->input('pageNo', 1);

        $pageSize = $request->input('pageSize', 15);

        $list = TradeLogic::queryLogs($user_id, $pageSize, $pageNo);

        if(false === $list) {
            return $this->error(TradeLogic::getErrorMessage());
        }

        return $this->success($list);
    }

    /**
     * @notes 划转
     * @param HandleTransferRequest $request
     * @return JsonResponse
     * @author Evil丿
     * @date 2025/04/21 17:08
     */
    public function handleTransfer(HandleTransferRequest $request)
    {
        $from = $request -> input('from');

        $coin = $request->input('coin');

        $num = $request->input('num');

        $user_id = auth() -> user() -> id;

        $res = TradeLogic::handleTransfer($user_id, $coin, $num, $from);

        if(false === $res) {
            return $this->error(TradeLogic::getErrorMessage());
        }

        return $this->success();
    }
}
